#include "configurable.h"
#include "log.h"

Configurable::Configurable() {
}

Configurable::~Configurable() {
}

Configurable::ParameterList::ParameterList(){
}

void Configurable::ParameterList::print_element_names(xmlNode * a_node)
{
    xmlNode *cur_node = NULL;

    for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
        if (cur_node->type == XML_ELEMENT_NODE) {
            parameters_[std::string((const char*)cur_node->name)] = 
            					std::string((char*)cur_node->content);
        }

        print_element_names(cur_node->children);
    }
}


void Configurable::ParameterList::readConfigXML(const std::string& filepath){
	xmlDoc *doc = NULL;
    xmlNode *root_element = NULL;
    /*
     * this initialize the library and check potential ABI mismatches
     * between the version it was compiled for and the actual shared
     * library used.
     */
    LIBXML_TEST_VERSION

    /*parse the file and get the DOM */
    doc = xmlReadFile(filepath.c_str(), NULL, 0);

	CHECK_NE(doc, NULL)
		<< "Could not parse file " << filepath;
    
    /*Get the root element node */
    root_element = xmlDocGetRootElement(doc);

    print_element_names(root_element);
	/*free the document */
    xmlFreeDoc(doc);	
    
    /*
     *Free the global variables that may
     *have been allocated by the parser.
     */
    xmlCleanupParser();
}

std::string Configurable::ParameterList::get_param(const std::string& nume){
	std::map<std::string, std::string>::iterator iter= parameters_.find(nume);
	if (iter==parameters_.end()) return 0;
	return (*iter).first;
}

bool Configurable::ParameterList::set_parameter(const std::string& name, const std::string& value){
	std::map<std::string, std::string>::iterator iter= parameters_.find(name);
	if (iter==parameters_.end()) return false;
	parameters_[name] =value;
	return true;
}

bool Configurable::ParameterList::add_parameter(const std::string& name, const std::string& value){
	parameters_[name] = value;
	return true;
}

